/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.statements;

import org.databasefacade.select.Arithmetic;
import org.databasefacade.select.Comparison;
import org.databasefacade.select.Condition;
import org.databasefacade.select.From;
import org.databasefacade.select.Where;
import org.databasefacade.table.Table;
import org.databasefacade.table.column.Column;
import org.databasefacade.translator.Translator;
import org.databasefacade.update.Update;

public class UpdateStatement implements DatabaseStatement {
	private Update update;
	private From from;
	private Where where = new Where();
	private Translator translator;

	
	/**
	 * Creates an update statement for the indicated
	 * table. Update principal table cannot be aliased.
	 * 
	 * Override the table alias for the table being 
	 * used in the statement.
	 */
	public UpdateStatement(Table table) {
		table.setTableAlias(table.getTableName());
		update = new Update(table);
		from = new From(table);
		this.translator = table.getDBHelper().getTranslator();
	}
		
	/**
	 * Use this method to access the fields that must be selected
	 */
	public void set(Column target, Column source) {
		update.set(target,source);
	}
	
	public void set(Column target, Arithmetic calc) {
		update.set(target,calc);
	}
	
	public void set(Column target, SelectStatement selectStmt) {
		update.set(target,selectStmt);
	}
	
	public void set(Column target, CaseStatement caseStmt) {
		update.set(target,caseStmt);
	}
	
	public void set(Column target, Object value) {
		update.set(target,value);
	}
	
	/**
	 * Use this method to set from clause
	 */
	public From from() {
		return from;
	}
	
	/**
	 * Use this method to set where clause
	 */
	public Condition where(Comparison compare) {
		where.first(compare);
		return where.conditions();
	}
	public Condition where() {
		return where.conditions();
	}
	
	/**
	 * Return the generated SQL for this statement
	 */
	public String getSQL() {
		return translator.getUpdateSQL(update, from, where);
	}
}
